package com.lizongbo.seotijian.rules.whois.domainhandlers;

import com.lizongbo.seotijian.rules.whois.DomainHandler;
import com.lizongbo.seotijian.rules.whois.WhoisClient;
import com.lizongbo.seotijian.util.HttpUtil;

public class BADomainHandler implements DomainHandler {
	@Override
	public String getTld() {
		return ".ba";
	}

	/**
	 * 这个网站的whois信息是图片的
	 */
	@Override
	public String getWhoisInfo(String domain) {
		String urlStr = "http://www.nic.ba/stream/whois/index.php";
		String query = "wun=1&naziv=google&ekstenzija=1&submit=Poka%9Ei+WHOIS+podatke";
		String ss[] = new String[] { "____", "ba", "org.ba", "net.ba",
				"gov.ba", "mil.ba", "edu.ba", "unsa.ba", "web.ba", };
		for (int i = 0; i < ss.length; i++) {
			if (domain.endsWith(ss[i])) {
				query = "wun=1&naziv="
						+ domain.substring(0, domain.indexOf(ss[i]) - 1)
						+ "&ekstenzija=" + i + "&submit=Poka%9Ei+WHOIS+podatke";
			}
		}
		String txt = "";
		try {
			String img = "";
			txt = HttpUtil.httpPost(urlStr, query,
					"http://www.nic.ba/stream/whois/");
			int beginIndex = txt.indexOf("whois_show.php?did=");
			if (beginIndex > 0) {
				return "http://www.nic.ba/stream/whois/"
						+ txt.substring(beginIndex, txt
								.indexOf("'", beginIndex));
			}
			txt = WhoisClient.trimEmptyLine(WhoisClient.html2txt(txt));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return txt;
	}
}